<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="database" scope="request" class="accessibleworldsdatabase.AccessibleWorldsDatabaseJavaBeans"/>
<jsp:useBean id="des" scope="request" class="accessibleworldsdes.AccessibleWorldsDESJavaBeans"/>
<jsp:useBean id="mail" scope="request" class="accessibleworldsmail.AccessibleWorldsMailJavaBeans"/>
<%try {

String accessCode = request.getParameter("accessCode");

if(!(accessCode==null || "".equals(accessCode))) {
	
    String secureAccessCode = des.decryptString(session.getId(), accessCode);
    secureAccessCode = URLDecoder.decode(secureAccessCode, "UTF-8");
    
    if(des.getEmailAccessCode().equals(accessCode)){
    	
        String email = request.getParameter("email");
        String validationCode = request.getParameter("validationCode");

        if(!(email==null || "".equals(email) || validationCode==null || "".equals(validationCode))) {
            
        	validationCode = des.decryptString(des.getServiceSecretKey(), validationCode);
        	validationCode = URLDecoder.decode(validationCode, "UTF-8");
            
            if(validationCode.equals(email)) {
            	
                database.connect(des.decryptString(des.getServiceSecretKey(), database.getEncryptedPassword()));
                
                String SQL =
                "SELECT * FROM Users"+
                	" WHERE"+
                		" email = \""+email+"\""+
                		" AND U_type = \"1\"";
                
                ResultSet resultSet = database.execSQL(SQL);
                
                if (resultSet.next()) {
                    SQL =
					"UPDATE Users"+
                    	" SET"+
							" U_type = \"2\""+
                    	" WHERE"+
							" email = \""+email+"\""+
                    		" AND U_type = \"1\"";
                    
                    database.updateSQL(SQL);

                    out.print(email+" is activated.");
                }
                else {
                    SQL =
					"SELECT * FROM Users"+
                    	" WHERE"+
							" email = \""+email+"\""+
                    		" AND (U_type = \"2\" OR U_type = \"3\")";
                    
                    resultSet = database.execSQL(SQL);
                    if(resultSet.next()) {
                        out.print(email+" is already activated.");
                    }
                }
                
                database.closeConnection();
            }
        }
    }
    else if(des.getAndroidAccessCode().equals(secureAccessCode) || des.getComputerAccessCode().equals(secureAccessCode)) {
    	
        String email = request.getParameter("email");
        if(!(email==null || "".equals(email))) {
            
            email = des.decryptString(session.getId(), email);
            email = URLDecoder.decode(email, "UTF-8");
            
            String validationCode = email;
            validationCode = URLEncoder.encode(validationCode, "UTF-8");
            validationCode = des.encryptString(des.getServiceSecretKey(), validationCode);

            String url = getServletContext().getInitParameter("validationAddress");
            url = url + "?accessCode="+URLEncoder.encode(des.getEmailAccessCode(), "UTF-8");
            url = url + "&email="+URLEncoder.encode(email, "UTF-8");
            url = url + "&validationCode="+URLEncoder.encode(validationCode+"", "UTF-8");
            
            mail.sendMail(des.decryptString(des.getServiceSecretKey(), mail.getEncryptedSenderMailPassword()),email, "Activation Mail", url, null);

            String output = URLEncoder.encode("OK","UTF-8");
            output = des.encryptString(session.getId(),output);
            out.print(output);
        }
    }
}
} catch (Exception ex) {}%>